package com.franzliszt.OrderOnline.SQLite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.hospitalnumberingsystem.R;


import java.util.ArrayList;
import java.util.List;

public class Dao {
    public static final String TAG = "DataBase";
    private Helper helper ;
    private SQLiteDatabase DB;
    public Dao(Context context)
    {
        helper = new Helper(context);
    }
    public void Insert(GoodsList goodsList){
        DB = helper.getReadableDatabase();
        if (DB.isOpen())
        {
            ContentValues values = new ContentValues();
            values.put( Helper.Row_User,goodsList.getUserName());
            values.put( Helper.Row_PassWord,goodsList.getPassWord());
            values.put( Helper.Row_GoodsImg,goodsList.getImg());
            values.put( Helper.Row_GoodsName,goodsList.getName());
            values.put( Helper.Row_GoodsNumber,goodsList.getNumber());
            values.put( Helper.Row_GoodsPrice,goodsList.getPrice());
            values.put( Helper.Row_GoodsSub,goodsList.getSub() );
            values.put( Helper.Row_GoodsAdd,goodsList.getAdd() );
            long RowId = DB.replace(Helper.TableName,null,values);
            if(RowId == -1)
                Log.i(TAG, "数据插入失败！");
            else
                Log.i(TAG, "数据插入成功！"+RowId);
        }
    }
    //根据序号删除数据
    //当前id为暂不为序号
    public void Delete(){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            String whereClause = null;
            String[] whereArgs = null;
            int count = DB.delete(Helper.TableName, whereClause, whereArgs);
            if (count > 0)
                Log.i(TAG, "删除了: " + count + "行");
            else
                Log.i(Helper.TableName, "数据未删除！");
            DB.close(); // 数据库关闭
        }
    }
    public void UpdatePassWord(String UserName,String PassWord){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            ContentValues values = new ContentValues();
            values.put(Helper.Row_PassWord,PassWord);
            int count = DB.update(Helper.TableName,values,"UserName = ?",new String[]{UserName});
            if (count > 0){
                Log.d(TAG,"update password row="+count);
            }else {
                Log.d(TAG,"update fail");
            }
            DB.close();
        }
    }
//    public void Update(Account account){
//        DB = helper.getWritableDatabase();
//        if(DB.isOpen()) {   // 如果数据库打开, 执行更新的操作
//            ContentValues values = new ContentValues();
//            //values.put( Helper.Row_Account,account.getUserID());
//            values.put( Helper.Row_UserName,account.getUserName());
//            //values.put( Helper.Row_PassWord,account.getPassWord());
//            values.put( Helper.Row_Sex,account.getSex());
//            values.put( Helper.Row_PhoneNumber,account.getPhoneNumber());
//            int count  = DB.update(Helper.TableName, values, "Account = ?", new String[]{account.getUserID() + ""});
//            if (count > 0)
//                Log.i(TAG, "修改了: " + count + "行");
//            else
//                Log.i(Helper.TableName, "数据未删除！");
//            DB.close(); // 数据库关闭
//        }
//    }
//    public String Query(String Account){
//        DB = helper.getReadableDatabase();
//        //selection查询子句的条件，可以使用主键查询
//        String[] columns = {Helper.Row_Account,Helper.Row_PassWord};  // 需要的列
//        String selection = "Account = ?";
//        String[] selectionArgs = {Account + ""};
//        Cursor cursor = DB.query(Helper.TableName,columns,selection,selectionArgs,null,null,null);
//        if (cursor.moveToFirst())
//        {
//                //String _id = cursor.getString(0);
//                String Content = cursor.getString(cursor.getColumnIndex( Helper.Row_PassWord ));
//                return Content;
//        }
//        DB.close();
//        return null;
//    }
    public List<GoodsList> QueryAll() {
        DB = helper.getReadableDatabase();  // 获得一个只读的数据库对象
        if(DB.isOpen()) {
            String[] columns = {Helper.Row_User,Helper.Row_PassWord,Helper.Row_GoodsImg, Helper.Row_GoodsName,Helper.Row_GoodsNumber,Helper.Row_GoodsPrice};  // 需要的列
            String selection = null;    // 选择条件, 给null查询所有
            String[] selectionArgs = null;  // 选择条件的参数, 会把选择条件中的? 替换成数据中的值
            String groupBy = null;  // 分组语句  group by name
            String having = null;   // 过滤语句
            String orderBy = null;  // 排序
            Cursor cursor = DB.query(Helper.TableName, columns, selection, selectionArgs, groupBy, having, orderBy);
            String user;
            String password;
            int mImg;
            String mName;
            float mPrice;
            int mNumber;
            int Add;
            int Sub;
            if(cursor != null && cursor.getCount() > 0) {
                List<GoodsList> goodsLists = new ArrayList<GoodsList>();
                while(cursor.moveToNext()) {    // 向下移一位, 知道最后一位, 不可以往下移动了, 停止.
                    user = cursor.getString(cursor.getColumnIndex( Helper.Row_User ));
                    password = cursor.getString(cursor.getColumnIndex( Helper.Row_PassWord ));
                    mImg = cursor.getInt(cursor.getColumnIndex( "GoodsImg" ));
                    mName = cursor.getString(cursor.getColumnIndex( "GoodsName" ));
                    mPrice = cursor.getFloat(cursor.getColumnIndex( "GoodsPrice"));
                    mNumber = cursor.getInt(cursor.getColumnIndex( "GoodsNumber"));
                    Add =  R.drawable.icon_add;
                    Sub = R.drawable.icon_sub;
                    goodsLists.add(new GoodsList(user,password,mImg,mName,mPrice,mNumber,Sub,Add));
                }
                DB.close();
                return goodsLists;
            }
            DB.close();
        }
        return null;
    }
}
